package org.funsciencewithyourcomputer.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;

public class DictionaryLoading {

	public static final boolean DEBUG = false;

	static final String URL = DEBUG ? "/"
			: "http://www.funsciencewithyourcomputer.org/gwt/steg/";

	static void readDictionary(final Dictionary dictionary) {
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL
				+ NLS.get(NLS.DICTIONARY));

		try {
			Steg.statusLabel.setText(NLS.get(NLS.PROGRESS_LOAD_DICTIONARY));
			Request request = builder.sendRequest(null, new RequestCallback() {
				public void onError(Request request, Throwable exception) {
					Steg.setStatus("Error 3: "+exception, true);
				}

				public void onResponseReceived(Request request,
						Response response) {
					if (200 == response.getStatusCode()) {
							dictionary.setWords(response.getText());
					} else {
						Steg.setStatus("Error 2: "+response.getStatusCode(), true);
					}
				}
			});
		} catch (RequestException e) {
			Steg.setStatus("Error 1"+e, true);
		}
	}
}
